<?php

function remittancedoRateRequest($RequestAction,$_tmpRemittanceID,$RemittanceSentAmount, $SenderID, $ReceiverID,$RateID,$RateRequestedValue,$RatePointValue) {
    global $sysTime, $SESSID, $SESSCD;
    $prStr = '';
    $whereCause = "WHERE req_Tmp = $_tmpRemittanceID AND req_htr = $RateID AND req_Amount = '$RemittanceSentAmount'
					AND sysUser = $_SESSION[$SESSID] AND sysOffice = $_SESSION[$SESSCD]
				   AND req_Cli = $SenderID AND req_Bnf = $ReceiverID";
    if ($RequestAction == 1) {
        $GLOBALS['MYT']->db_query("UPDATE hd_cambreqday SET Status = 3 {$whereCause}");
        $GLOBALS['MYT']->db_query("UPDATE hd_cambreq SET Status = 3 {$whereCause}");
        $prStr .= '<script charset="utf-8" type="text/javascript">
            document.InvoiceProperties.action=\'_response/call.php?get=Operative/remittancecalc\';
            subform(\'InvoiceProperties\',\'inv_Result\');</script>';
    } else {
        $GET = $GLOBALS['MYT']->db_array("SELECT Status FROM hd_cambreqday {$whereCause} AND Status != 3");
        if (!isset($GET[1])) {
            $GETRateConfig = $GLOBALS['MYT']->db_array("SELECT ast_Value, htr_Points FROM systemoffice.administration_settings INNER JOIN officeschema.hd_cambiosdia ON htr_ID = {$RateID} WHERE ast_ID = 1");
            $GETRMV = $GLOBALS['MYT']->db_array("SELECT sysTime, Status FROM hd_cambreqday {$whereCause} AND Status = 3");
            if (!isset($GETRMV[1])) {
                if ($GETRateConfig[1]['ast_Value'] == 2 && $GETRateConfig[1]['htr_Points'] >= $RateRequestedValue && $RateRequestedValue > 0) {
                    $GLOBALS['MYT']->db_query("INSERT INTO hd_cambreq (sysUser, sysTime, req_Pts, req_htr, req_Cli, req_Bnf, req_Tmp, req_Rate, sysOffice, req_Amount,Status,apr_Rate,apr_Pts,aprTime,aprUser) VALUES ({$_SESSION[$SESSID]},'{$sysTime}','{$RateRequestedValue}','{$RateID}','{$SenderID}','{$ReceiverID}','{$_tmpRemittanceID}','{$RatePointValue}',{$_SESSION[$SESSCD]},'{$RemittanceSentAmount}',2,'{$RatePointValue}','{$RateRequestedValue}','{$sysTime}',0)");
                    $GLOBALS['MYT']->db_query("INSERT INTO hd_cambreqday (sysUser, sysTime, req_Pts, req_htr, req_Cli, req_Bnf, req_Tmp, req_Rate, sysOffice, req_Amount,Status,apr_Rate,apr_Pts,aprTime,aprUser) VALUES ({$_SESSION[$SESSID]},'{$sysTime}','{$RateRequestedValue}','{$RateID}','{$SenderID}','{$ReceiverID}','{$_tmpRemittanceID}','{$RatePointValue}',{$_SESSION[$SESSCD]},'{$RemittanceSentAmount}',2,'{$RatePointValue}','{$RateRequestedValue}','{$sysTime}',0)");
                    $prStr .= '<script charset="utf-8" type="text/javascript">
                        document.InvoiceProperties.action=\'_response/call.php?get=Operative/remittancecalc\';
			subform(\'InvoiceProperties\',\'inv_Result\');</script>';
                } else {
                    $GLOBALS['MYT']->db_query("INSERT INTO hd_cambreq (sysUser, sysTime, req_Pts, req_htr, req_Cli, req_Bnf, req_Tmp, req_Rate, sysOffice, req_Amount) VALUES ({$_SESSION[$SESSID]},'{$sysTime}','{$RateRequestedValue}','{$RateID}','{$SenderID}','{$ReceiverID}','{$_tmpRemittanceID}','{$RatePointValue}',{$_SESSION[$SESSCD]},'{$RemittanceSentAmount}')");
                    $GLOBALS['MYT']->db_query("INSERT INTO hd_cambreqday (sysUser, sysTime, req_Pts, req_htr, req_Cli, req_Bnf, req_Tmp, req_Rate, sysOffice, req_Amount) VALUES ({$_SESSION[$SESSID]},'{$sysTime}','{$RateRequestedValue}','{$RateID}','{$SenderID}','{$ReceiverID}','{$_tmpRemittanceID}','{$RatePointValue}',{$_SESSION[$SESSCD]},'{$RemittanceSentAmount}')");
                }
            } else {
                if ($GETRateConfig[1]['ast_Value'] == 2 && $GETRateConfig[1]['htr_Points'] >= $RateRequestedValue && $RateRequestedValue > 0) {
                    $GLOBALS['MYT']->db_query("UPDATE hd_cambreq SET sysTime = '{$sysTime}', req_Pts = '{$RateRequestedValue}', req_htr = '{$RateID}', req_Rate = '{$RatePointValue}', Status = 2, apr_Rate = '{$RatePointValue}',apr_Pts = '{$RateRequestedValue}',aprTime = '{$sysTime}',aprUser = 0
									WHERE sysTime = '{$GETRMV[1]['sysTime']}' AND sysUser = {$_SESSION[$SESSID]} AND req_Cli = {$SenderID} AND req_Bnf = {$ReceiverID} AND req_Amount = '{$RemittanceSentAmount}' AND req_Tmp = {$_tmpRemittanceID} AND sysOffice = {$_SESSION[$SESSCD]}");
                    $GLOBALS['MYT']->db_query("UPDATE hd_cambreqday SET sysTime = '{$sysTime}', req_Pts = '{$RateRequestedValue}', req_htr = '{$RateID}', req_Rate = '{$RatePointValue}', Status = 2, apr_Rate = '{$RatePointValue}',apr_Pts = '{$RateRequestedValue}',aprTime = '{$sysTime}',aprUser = 0
									WHERE sysTime = '{$GETRMV[1]['sysTime']}' AND sysUser = {$_SESSION[$SESSID]} AND req_Cli = {$SenderID} AND req_Bnf = {$ReceiverID} AND req_Amount = '{$RemittanceSentAmount}' AND req_Tmp = {$_tmpRemittanceID} AND sysOffice = {$_SESSION[$SESSCD]}");
                    $prStr .= '<script charset="utf-8" type="text/javascript">
                        document.InvoiceProperties.action=\'_response/call.php?get=Operative/remittancecalc\';
			subform(\'InvoiceProperties\',\'inv_Result\');</script>';
                } else {

                    $GLOBALS['MYT']->db_query("UPDATE hd_cambreq SET sysTime = '{$sysTime}', req_Pts = '{$RateRequestedValue}', req_htr = '{$RateID}', req_Rate = '{$RatePointValue}', Status = 1, apr_Rate = null,apr_Pts = null,aprTime = null,aprUser = null
									WHERE sysTime = '{$GETRMV[1]['sysTime']}' AND sysUser = {$_SESSION[$SESSID]} AND req_Cli = {$SenderID} AND req_Bnf = {$ReceiverID} AND req_Amount = '{$RemittanceSentAmount}' AND req_Tmp = {$_tmpRemittanceID} AND sysOffice = {$_SESSION[$SESSCD]}");
                    $GLOBALS['MYT']->db_query("UPDATE hd_cambreqday SET sysTime = '{$sysTime}', req_Pts = '{$RateRequestedValue}', req_htr = '{$RateID}', req_Rate = '{$RatePointValue}', Status = 1, apr_Rate = null,apr_Pts = null,aprTime = null,aprUser = null
									WHERE sysTime = '{$GETRMV[1]['sysTime']}' AND sysUser = {$_SESSION[$SESSID]} AND req_Cli = {$SenderID} AND req_Bnf = {$ReceiverID} AND req_Amount = '{$RemittanceSentAmount}' AND req_Tmp = {$_tmpRemittanceID} AND sysOffice = {$_SESSION[$SESSCD]}");
                }
            }
            $GET = $GLOBALS['MYT']->db_array("SELECT Status FROM hd_cambreqday {$whereCause} AND Status != 3");
        }
    }
    if (isset($GET[1])) {
        $prStr .= rt_field('hidden', 'ED', 'rtreq_cancel', '', '', '', '', '');
        if ($GET[1]['Status'] == 1) {
            $prStr .= 'A Aguardar | ';
            $prStr .= rt_field('button', 'ED', 'ref_refresh', 'Verificar', '', '', '', '"
			onclick="document.InvoiceProperties.action=\'_response/call.php?get=Operative/remittancecalc\';
			subform(\'InvoiceProperties\',\'inv_Result\');');
            $prStr .= rt_field('button', 'ED', 'ref_cancel', 'Cancelar', '', '', '', '"
			onclick="document.InvoiceProperties.action=\'_response/call.php?get=Operative/opv_remittancerate\';
			document.InvoiceProperties.rtreq_cancel.value=1;
			subform(\'InvoiceProperties\',\'ORRate\');');
        } elseif ($GET[1]['Status'] == 2) {
            $prStr .= '<div class="Confirm">Aceite</div>';
        } elseif ($GET[1]['Status'] == 5) {
            $prStr .= '<div class="Error">Rejeitado</div>';
        }
    }
    return $prStr;
}
if(!isset($_POST['rtreq_cancel'])){$_POST['rtreq_cancel']=0;}
if(!isset($_POST['PTMod'])){$_POST['PTMod']=0;}
if(!isset($_POST['RTMod'])){$_POST['RTMod']=0;}
$RateResponse = remittancedoRateRequest($_POST['rtreq_cancel'],$_POST['TmpOp'],str_replace(',', '.', $_POST['FraCalc']), $_POST['OBJCTCL'], $_POST['OBJCTRC'],$_POST['htr_ID'],$_POST['PTMod'],$_POST['RTMod']);
if(!isset($prStr)){echo $RateResponse; } else {$prStr .= $RateResponse;}